#include "dialog.h"

Dialog::Dialog()
{
}

Dialog::~Dialog()
{
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		delete *it;
	}

	m_element_list.clear();
}

void Dialog::AddElement(GuiElement* element)
{
	m_element_list.push_back(element);
}

bool Dialog::ElementExists(std::string id)
{
	// Try to find the element. If it was found, return true.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
		if ((*it)->id() == id)
			return true;

	return false;
}

bool Dialog::GetElement(std::string id, GuiElement*& element)
{
	// Try to find the element. If it was found, return true.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		if ((*it)->id() == id)
		{
			element = *it;
			return true;
		}
	}

	// Return false if the element was not part of the dialog.
	return false;
}

void Dialog::RemoveElement(std::string id)
{
	for (std::list<GuiElement*>::iterator element_it = m_element_list.begin();
		element_it != m_element_list.end(); element_it++)
	{
		// If we found the element to be deleted.
		if ((*element_it)->id() == id)
		{
			delete (*element_it);
			m_element_list.erase(element_it);
			break;
		}
	}
}

void Dialog::RemoveElementOnNextUpdate(std::string id)
{
	m_elements_to_remove.push_back(id);
}

void Dialog::PrimaryMouseButtonUp(int x, int y)
{
	// Delegate to every element.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		(*it)->PrimaryMouseButtonUp(x, y);
	}
}

void Dialog::PrimaryMouseButtonDown(int x, int y)
{
	// Delegate to every element.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		(*it)->PrimaryMouseButtonDown(x, y);
	}
}

void Dialog::SecondaryMouseButtonUp(int x, int y)
{
	// Delegate to every element.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		(*it)->SecondaryMouseButtonUp(x, y);
	}
}

void Dialog::SecondaryMouseButtonDown(int x, int y)
{
	// Delegate to every element.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		(*it)->SecondaryMouseButtonDown(x, y);
	}
}

void Dialog::MouseAxisChange(int x, int y)
{
	// Delegate to every element.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		(*it)->MouseAxisChange(x, y);
	}
}

void Dialog::CharEntered(int keycode, int unichar)
{
	// Delegate to every element.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		(*it)->CharEntered(keycode, unichar);
	}
}

void Dialog::Update()
{
	// Remove every element which is to be deleted.
	for (std::list<std::string>::iterator it = m_elements_to_remove.begin(); it != m_elements_to_remove.end(); it++)
	{
		std::string id = *it;
		for (std::list<GuiElement*>::iterator element_it = m_element_list.begin();
			element_it != m_element_list.end(); element_it++)
		{
			// If we found the element to be deleted.
			if ((*element_it)->id() == id)
			{
				delete (*element_it);
				m_element_list.erase(element_it);
				break;
			}
		}
	}

	// Clear the list of elements to delete, we have deleted everything in it.
	m_elements_to_remove.clear();

	// Delegate Update() to every element.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		(*it)->Update();
	}
}

void Dialog::Draw()
{
	// Delegate Draw() to every element.
	for (std::list<GuiElement*>::iterator it = m_element_list.begin(); it != m_element_list.end(); it++)
	{
		(*it)->Draw();
	}
}
